﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace WindowsGame4
{
    class Enemy_StopState : State
    {
        private static Enemy_StopState instance;

        private Enemy_StopState()
        {
            base.Name = "StopState";
        }

        public override void enter(AIObject aiObject)
        {
            //System.Console.WriteLine("stopenterstate: " + aiObject.Id);
            //Console.WriteLine("<Projectile_DestroyState> enter");
            
            AIEnemy aiEnemy = (AIEnemy)aiObject;
            Enemy enemy = (Enemy)aiEnemy.Enemy;
            if (enemy.IdAnimation == "move_left")
                enemy.changeAnimation("left");
            if (enemy.IdAnimation == "move_right")
                enemy.changeAnimation("right");
            if (enemy.IdAnimation == "move_down")
                enemy.changeAnimation("front");
            if (enemy.IdAnimation == "move_up")
                enemy.changeAnimation("back");
            //System.Console.WriteLine("exitstopenterstate");
        }

        public static Enemy_StopState getInstance()
        {
            if (instance == null)
                instance = new Enemy_StopState();
            return instance;
        }

        public override void execute(AIObject aiObject)
        {
            //System.Console.WriteLine("stopstate");
            AIEnemy aiEnemy = (AIEnemy)aiObject;
            Enemy enemy = (Enemy)aiEnemy.Enemy;
            GameInfo gameInfo = GameInfo.getInstance();
            
            // Check if the enemy can attack
            if (enemy.ReloadTime > 0)
                enemy.ReloadTime -= enemy.Level;
            else
                enemy.Attack = true;

            // Check if the is a bomb near of the enemy
            Object bomb = enemy.checkBomb();
            if (bomb != null)
            {
                gameInfo.moveRandomEnemy(enemy,6);
                EventEmitter.getInstance().registerMessage(0, aiEnemy.Id, aiEnemy.Id, Message.MessageType.msg_move);
            }
            else
            {
                // Check if there are a enemy in the vision
                Object obj = gameInfo.detectEnemy(enemy);
                if (obj != null && enemy.GunAmmo > 0)
                {
                    // Attack
                    
                    gameInfo.attackGun(enemy);
                }
                else
                {
                    // Detect item
                    Cell cellItem = gameInfo.detectItem(enemy);
                    if (cellItem != null)
                    {
                        gameInfo.moveEnemy(enemy, cellItem.Row, cellItem.Column);
                        EventEmitter.getInstance().registerMessage(0, aiEnemy.Id, aiEnemy.Id, Message.MessageType.msg_move);
                    }
                    else
                    {
                        Random r = new Random(DateTime.Now.Millisecond);
                        int num = r.Next(1, 7);
                        Boolean boxDetected = false;
                        if (num == 3)
                        {
                            //boxDetected = gameInfo.detectBox(enemy);
                            if (enemy.BombAmmo > 0)
                            {
                                // Put bomb
                                gameInfo.attackBomb(enemy);
                                gameInfo.moveRandomEnemy(enemy,8);
                                EventEmitter.getInstance().registerMessage(0, aiEnemy.Id, aiEnemy.Id, Message.MessageType.msg_move);
                            }
                        }
                        if (boxDetected == false)
                        {
                            // Move
                            gameInfo.moveRandomEnemy(enemy,3);
                            EventEmitter.getInstance().registerMessage(0, aiEnemy.Id, aiEnemy.Id, Message.MessageType.msg_move);
                        }
                    }
                }
            }
            //System.Console.WriteLine("exitstopstate");
        }

        public override void exit(AIObject aiObject)
        {
            
        }

        public override Boolean onMessage(AIObject aiObject, Message message)
        {
            //System.Console.WriteLine("stop receive message");
            Boolean correct = false;
            switch (message.Type)
            {
                case Message.MessageType.msg_move:
                    {
                        aiObject.StateMachine.changeState(Enemy_MoveState.getInstance());
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_follow:
                    {
                        aiObject.StateMachine.changeState(Enemy_MoveState.getInstance());
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_attacked:
                    {
                        AIEnemy aiEnemy = (AIEnemy)aiObject;
                        aiObject.StateMachine.changeState(Enemy_DestroyState.getInstance());
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_disableBlinking:
                    {
                        AIEnemy aiEnemy = (AIEnemy)aiObject;
                        Enemy enemy = (Enemy)aiEnemy.Enemy;
                        enemy.Blinking = false;
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_newShoot:
                    {
                        AIEnemy aiEnemy = (AIEnemy)aiObject;
                        aiEnemy.Enemy.Attack = true;
                        correct = true;
                        break;
                    }
            }
            return correct;
        }
    }
}
